package com.kayak.easysp.appsecurity.trans;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.rpc.RpcException;
import com.kayak.dbutil.sql.SqlException;
import com.kayak.dubbo.bean.ConsumerBean;
import com.kayak.dubbo.bean.RpcBean;
import com.kayak.easysp.appsecurity.SecurityBase;
import com.kayak.easysp.transbase.TransBean;
import com.kayak.easysp.util.ConsumerBeanUtil;
import com.kayak.easysp.util.ErrorCodeEasysp;
import com.kayak.easysp.util.TransConstEasysp;
import com.kayak.easysp.util.TransUtilEasysp;
import com.kayak.exception.ErrorException;
import com.kayak.kkutils.strutil.StringUtil;
import com.kayak.service.security.secretkey.GetMainKeyInterface;


/**
* 同步主密钥
* @author songqm	
* @Time	2016年8月9日 16:00:51
* 
* 1、检查数据head_osnumber
* 2、调用服务活的主密钥
* 3、返回前端
*/
@Component
public class S010101 extends SecurityBase{

	private static final Logger log = LoggerFactory.getLogger(S010101.class);

	@Autowired
	GetMainKeyInterface getMainKeyService;

	@Override
	public void initParam(TransBean transBean) throws ErrorException {
		log.info("====================开始请求主密钥");
	}

	@Override
	public void dataCheck(TransBean transBean) throws ErrorException {
		StringUtil.checkNeedDatas("head_osnumber", transBean.getLocalMap());
		
	}

	@Override
	public void doBusi(TransBean transBean) throws ErrorException,
			SqlException, Exception {
		
		 RpcBean rpcBean=null;
		 ConsumerBean consumerBean = ConsumerBeanUtil.getConsumerBean(transBean);
	     consumerBean.addRequest("head_osnumber",transBean.getLocal("head_osnumber"));
	     consumerBean.addRequest("flag", TransConstEasysp.KEY_STATUS_FLAG_FIRST);
	 	try {
			rpcBean=getMainKeyService.execute(consumerBean);
		} catch (RpcException e) {
			throw new ErrorException(ErrorCodeEasysp.ERROR_9990_CODE,ErrorCodeEasysp.ERROR_9990_MSG);
		}
		
	 	TransUtilEasysp.verifyServiceReturn(rpcBean);
		
		log.info("====================主密钥："+rpcBean.getResponseParam("base_key"));
		
		transBean.getResponse().putAll(rpcBean.getResponse());
		
		transBean.addResponse(ErrorCodeEasysp.RET_CODE, ErrorCodeEasysp.RET_SUC_CODE);
		transBean.addResponse(ErrorCodeEasysp.RET_MSG, ErrorCodeEasysp.RET_SUC_MSG);
	}

}